package com.it.project.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.it.common.annotation.Log;
import com.it.common.core.controller.BaseController;
import com.it.common.core.domain.AjaxResult;
import com.it.common.enums.BusinessType;
import com.it.project.domain.UserProject;
import com.it.project.service.IUserProjectService;
import com.it.common.utils.poi.ExcelUtil;
import com.it.common.core.page.TableDataInfo;

/**
 * 用户项目关联Controller
 * 
 * @author it
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/userProject")
public class UserProjectController extends BaseController
{
    @Autowired
    private IUserProjectService userProjectService;

    /**
     * 查询用户项目关联列表
     */
    @PreAuthorize("@ss.hasPermi('userProject:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserProject userProject)
    {
        startPage();
        List<UserProject> list = userProjectService.selectUserProjectList(userProject);
        return getDataTable(list);
    }

    /**
     * 导出用户项目关联列表
     */
    @PreAuthorize("@ss.hasPermi('userProject:export')")
    @Log(title = "用户项目关联", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(UserProject userProject)
    {
        List<UserProject> list = userProjectService.selectUserProjectList(userProject);
        ExcelUtil<UserProject> util = new ExcelUtil<UserProject>(UserProject.class);
        return util.exportExcel(list, "用户项目关联数据");
    }

    /**
     * 获取用户项目关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('userProject:query')")
    @GetMapping(value = "{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userProjectService.selectUserProjectById(id));
    }

    /**
     * 新增用户项目关联
     */
    @PreAuthorize("@ss.hasPermi('userProject:add')")
    @Log(title = "用户项目关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserProject userProject)
    {
        // 检查用户是否已有项目权限
        if (userProjectService.checkUserProjectExists(userProject))
        {
            return error("该用户已关联到该项目，请不要重复添加");
        }
        return toAjax(userProjectService.insertUserProject(userProject));
    }

    /**
     * 修改用户项目关联
     */
    @PreAuthorize("@ss.hasPermi('userProject:edit')")
    @Log(title = "用户项目关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserProject userProject)
    {
        return toAjax(userProjectService.updateUserProject(userProject));
    }

    /**
     * 删除用户项目关联
     */
    @PreAuthorize("@ss.hasPermi('userProject:remove')")
    @Log(title = "用户项目关联", businessType = BusinessType.DELETE)
    @DeleteMapping("{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userProjectService.deleteUserProjectByIds(ids));
    }

    /**
     * 根据用户ID查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('userProject:list')")
    @GetMapping("/projectsByUser/{userId}")
    public AjaxResult getProjectsByUser(@PathVariable Long userId)
    {
        List<UserProject> list = userProjectService.selectProjectsByUserId(userId);
        return success(list);
    }

    /**
     * 根据项目ID查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('userProject:list')")
    @GetMapping("/usersByProject/{proId}")
    public AjaxResult getUsersByProject(@PathVariable Long proId)
    {
        List<UserProject> list = userProjectService.selectUsersByProId(proId);
        return success(list);
    }

    /**
     * 检查用户是否有指定项目的权限
     */
    @GetMapping("/checkPermission/{userId}/{proId}")
    public AjaxResult checkUserProjectPermission(@PathVariable Long userId, @PathVariable Long proId)
    {
        boolean hasPermission = userProjectService.checkUserProjectPermission(userId, proId);
        return success(hasPermission);
    }

    /**
     * 获取用户在项目中的权限等级
     */
    @GetMapping("/getPermissionLevel/{userId}/{proId}")
    public AjaxResult getUserProjectPermissionLevel(@PathVariable Long userId, @PathVariable Long proId)
    {
        Integer permissionLevel = userProjectService.getUserProjectPermissionLevel(userId, proId);
        return success(permissionLevel);
    }
}